# Maintainer: Sarah Ottinger <schalaalexiazeal@gmail.com>

_realname=tifffile
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=2021.1.8
pkgrel=1
pkgdesc='Read and write image data from and to TIFF files (mingw-w64)'
arch=('any')
url="https://github.com/cgohlke/tifffile"
license=('BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-python-numpy")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-imagecodecs: required only for encoding or decoding LZW, JPEG, etc."
            "${MINGW_PACKAGE_PREFIX}-python-matplotlib: required only for plotting"
            "${MINGW_PACKAGE_PREFIX}-python-lxml: required only for validating and printing XML")
source=("https://files.pythonhosted.org/packages/source/${_realname:0:1}/${_realname}/${_realname}-${pkgver}.tar.gz")
sha256sums=('b26e0373eca968f672043814a880700677ca1f6d1ce8a4e4d22ec8aea2bc0fcb')

prepare() {
  cd "$srcdir"
  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  msg "Python build for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
}
